using MeetingManagement.Domain.Entities;
using MeetingManagement.Domain.ValueObjects;
using Microsoft.EntityFrameworkCore;
using System.Reflection;

namespace MeetingManagement.Infrastructure
{
    public class MeetingManagementDbContext : DbContext
    {
        public MeetingManagementDbContext(DbContextOptions<MeetingManagementDbContext> options) : base(options) { }

        public DbSet<User> Users { get; set; }
        public DbSet<Meeting> Meetings { get; set; }
        public DbSet<MeetingRoom> MeetingRooms { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            
            // Ignore TimeSlot as a standalone entity
            modelBuilder.Ignore<TimeSlot>();

            // User configuration
            modelBuilder.Entity<User>(entity =>
            {
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Username).IsRequired().HasMaxLength(50);
                entity.Property(e => e.Email).IsRequired().HasMaxLength(100);
                entity.Property(e => e.PasswordHash).IsRequired();
                entity.Property(e => e.Role).HasMaxLength(50);

                entity.HasIndex(e => e.Username).IsUnique();
                entity.HasIndex(e => e.Email).IsUnique();
            });

            // Meeting configuration
            modelBuilder.Entity<Meeting>(entity =>
            {
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Title).IsRequired().HasMaxLength(200);
                entity.Property(e => e.Description).HasMaxLength(1000);
                entity.Property(e => e.Location).HasMaxLength(200);

                // Meeting与MeetingRoom关系
                entity.HasOne(e => e.MeetingRoom)
                    .WithMany()
                    .HasForeignKey(e => e.MeetingRoomId)
                    .IsRequired(false)
                    .OnDelete(DeleteBehavior.SetNull);
                
                // Configure TimeSlot as an owned type
                entity.OwnsOne(m => m.TimeSlot, ts =>
                {
                    ts.Property(p => p.Start).HasColumnName("StartTime");
                    ts.Property(p => p.End).HasColumnName("EndTime");
                    ts.WithOwner();
                });

                // 存储参与者列表为Json
                entity.Property(e => e.ParticipantIds)
                    .HasConversion(
                        v => string.Join(',', v),
                        v => v.Split(',', StringSplitOptions.RemoveEmptyEntries)
                             .Select(id => Guid.Parse(id))
                             .ToList());
            });

            // MeetingRoom configuration
            modelBuilder.Entity<MeetingRoom>(entity =>
            {
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Name).IsRequired().HasMaxLength(100);
                entity.Property(e => e.Location).HasMaxLength(200);
                entity.Property(e => e.Description).HasMaxLength(500);
            });
        }
    }
}
